//================================================
//===        UART_communication                ===
//================================================
/*                  下行数据协议					
下行协议定义						
Define	包头	Type	Comm	Data	包尾	备注
停止	FF	0x00	0x00	0x00	FF	
前进	FF	0x00	0x01	0x00	FF	
后退	FF	0x00	0x02	0x00	FF	
左转	FF	0x00	0x03	0x00	FF	
右转	FF	0x00	0x04	0x00	FF	
左前转	FF	0x00	0x05	0x00	FF	
左后转	FF	0x00	0x06	0x00	FF	
右前转	FF	0x00	0x07	0x00	FF	
右后转	FF	0x00	0x08	0x00	FF	
Kap	FF	0x03	Int	Dec	FF	 0<=Integral<=99
Kai	FF	0x04	Int	Dec	FF	0<=Decimal<=99
Kad	FF	0x05	Int	Dec	FF	
Ksp	FF	0x06	Int	Dec	FF	
Ksi	FF	0x07	Int	Dec	FF		
*///================================================
void Communication_Decode(void)
{
    if(Buffer[0]==0x00)
    {
        switch(Buffer[1])
        {
            case 0x01: Speed_Need=-100;Turn_Need = 0;Kai = 0.1;return;
            case 0x02: Speed_Need=100;Turn_Need = 0;Kai = 0.6; return;
            case 0x03: Turn_Need=80;Kai = 0.6;return;
            case 0x04: Turn_Need=-80;Kai = 0.6;return;
            case 0x00: Speed_Need=0;Turn_Need=0;Kai = 0.5;return;
            case 0x05:return;
            case 0x06:return;
            case 0x07:return;
            case 0x08:return;
            default: return;
        }
    }
    else if(Buffer[0]==0x01)
    {

    }
    else if(Buffer[0]==0x02)
    {
      
    }
    else if(Buffer[0]==0x03)
    {
        integral = Buffer[1] / 16 + Buffer[1] % 16;
        decimal = Buffer[2] / 16 + Buffer[2] % 16;
        Kap = integral + decimal/100.0;
    }
    else if(Buffer[0]==0x04)
    {
        integral = Buffer[1] / 16 + Buffer[1] % 16;
        decimal = Buffer[2] / 16 + Buffer[2] % 16;
        Kai = integral + decimal/100.0;      

    }
    else if(Buffer[0]==0x05)
    {
        integral = Buffer[1] / 16 + Buffer[1] % 16;
        decimal = Buffer[2] / 16 + Buffer[2] % 16;
        Kad = integral + decimal/100.0;

    }
    else if(Buffer[0]==0x06)
    {
        integral = Buffer[1] / 16 + Buffer[1] % 16;
        decimal = Buffer[2] / 16 + Buffer[2] % 16;
        Ksp = integral + decimal/100.0;

    }
    else if(Buffer[0]==0x07)
    {
        integral = Buffer[1] / 16 + Buffer[1] % 16;
        decimal = Buffer[2] / 16 + Buffer[2] % 16;
        Ksi = integral + decimal/100.0;
    }    
    else if(Buffer[0]==0x51)
    {
        if(Buffer[1]==0x00)
        {
            switch(Buffer[2])
            {
                  case 0x05: Speed_Need=-100;Turn_Need = 0;Kai = 0.1;return;
                  case 0x06: Speed_Need=100;Turn_Need = 0;Kai = 0.6; return;
                  case 0x07: Turn_Need=80;Kai = 0.6;return;
                  case 0x08: Turn_Need=-80;Kai = 0.6;return;
                  case 0x00: Speed_Need=0;Turn_Need=0;Kai = 0.5;return;
                  default : return;
            }
        }
        else
            return;
    }
    else
    {
        return;
    }
}
/*================================================
上行协议定义						
Define	包头	Type	Comm	Data	包尾	备注
	FF	0x01	Int	Dec	FF	 0<=Integral<=99
	FF	0x02	Int	Dec	FF	0<=Decimal<=99
	FF	0x03	Int	Dec	FF			
*///================================================
void Transmit_variable(byte Type,float Value)
{
  byte int0,int1,int2,int3,int4;
  Value = constrain(Value,0,99);
  Value = Value * 100;
  int1 = int(Value) / 1000;
  int2 = int(Value) % 1000 / 100;
  int3 = int(Value) % 1000 % 100 / 10;
  int4 = int(Value) % 1000 % 100 % 10 / 1;
  Transmit_byte[2] = Type;
  Transmit_byte[3] = int1;
  Transmit_byte[4] = int2;
  Transmit_byte[5] = int3;
  Transmit_byte[6] = int4;
  for(int i=0;i < 8;i++)
  {
     Serial.print(Transmit_byte[i],HEX);
  }
}
//================================================
//===                   Remote                ===
//================================================
void Remote()
{
  if (Serial.available()>0) //如果Arduino控制板的读缓冲区中存在上位机下达的字节
   {
        SBUF = Serial.read();
        delay(1);
        if(rec_flag==0)
        {
            if(SBUF==0xff)
            {
              rec_flag=1;
              index=0;
             }
         }
         else
         {
              if(SBUF==0xff)
              {
                  rec_flag=0;
                  if(index==3)
                  {
                      Communication_Decode();
                  }
                  index=0;
               }
               else
               {
                    Buffer[index]=SBUF;
                    index++;
                }
            }
      }
}
